package model.db.table;

import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Time;
import java.util.ArrayList;
import model.db.Execute;
import model.db.Insert;
import model.db.OurConnection;
import model.db.Select;

/**
 * Author: Raoux Etienne
 * Student login: jc218934
 * Student number: 12448190
 * email: etienne.raoux@my.jcu.edu.au
 * alt email: jc218934@gmail.com
 */
public class BidHistory extends Table
{
    private static int i =1;

    public static int selectNextId()
    {

        return Select.getInt("SELECT MAX (itemId) FROM BID_HISTORY")+1;
    }

    public BidHistory()
    {

    }

    public BidHistory getBidHistoryIfInDbElseNull()
    {
        String strToExecute = "SELECT * FROM BID_HISTORY WHERE item_url = "+itemUrl;
        Connection con = null;
        BidHistory item = null;
        ArrayList<BidHistory> bidHistoryList = new ArrayList<BidHistory>();
        try
        {
            con = OurConnection.connect();
            Statement stmt = con.createStatement();
            ResultSet resultset = stmt.executeQuery(strToExecute);

            while (resultset.next())
            {

                Integer itemId = resultset.getInt(1);
                String imgUrl = resultset.getString(2);
                String title = resultset.getString(3);
                String itemUrl = resultset.getString(4);
                String seller = resultset.getString(5);
                String sellerUrl = resultset.getString(6);
                Double price = resultset.getDouble(7);
                ArrayList<Bid> bidHistory = new ArrayList<Bid>();



                item = new BidHistory(itemId, imgUrl, title, itemUrl, seller, sellerUrl, price, bidHistory);
                break;
            }

            String strToExecute2 = "SELECT * FROM BID WHERE ITEM_ID = "+item.getItemId();
            ResultSet resultset2 = stmt.executeQuery(strToExecute2);
            while (resultset2.next())
            {
                Integer id = resultset2.getInt(1);
                Integer bidHistoryId = resultset2.getInt(2);
                Date date  = resultset2.getDate(3);
                Time time = resultset2.getTime(4);
                String bidder  = resultset2.getString(5);
                String bidType  = resultset2.getString(6);
                Integer qty  = resultset2.getInt(7);
                Double amt = resultset2.getDouble(8);


                item.add(new Bid(id, bidHistoryId, date, bidder, bidType, qty, amt));

            }

            con.close();

        }
        catch (Exception ex)
        {

        }

        return item;
    }



    @Override
    public void insertInDb()// todo check if it is already in the db
    {
        
        Insert.row(this);
        for (Bid bid : bidHistory)
        {
            bid.insertInDb();
        }
    }


    private String imgUrl;
    private String title;
    private String itemUrl;
    private String sellerUrl;
    private Integer itemId;

    public ArrayList<Bid> getBidHistory()
    {
        return bidHistory;
    }

    public String getImgUrl()
    {
        return imgUrl;
    }

    public String getItemUrl()
    {
        return itemUrl;
    }

    public Double getPrice()
    {
        return price;
    }

    public String getSellerName()
    {
        return sellerName;
    }

    public String getSellerUrl()
    {
        return sellerUrl;
    }

    public String getTitle()
    {
        return title;
    }
    private String sellerName;
    private Double price;
    private ArrayList<Bid> bidHistory;

    //CREATE TABLE "biddingHistory" ("url" TEXT, "title" TEXT, "itemUrl" TEXT,
    //"sellerUrl" TEXT, "price" INTEGER, "bidHistoryId" INTEGER PRIMARY KEY  NOT NULL)

    public BidHistory(Integer itemId, String imgUrl, String title, String itemUrl, String sellerUrl,
            String sellerName, Double price, ArrayList<Bid> bidHistory)
    {
        this.itemId = itemId;
        this.imgUrl = imgUrl;
        this.title = title;
        this.itemUrl = itemUrl;
        this.sellerUrl = sellerUrl;
        this.sellerName = sellerName;
        this.price = price;
        this.bidHistory = bidHistory;

    }

    @Override
    public String toString()
    {
        String str = "BidHistory: \n"
                + "imageUrl = "+imgUrl+"\n"
                + "title = "+title+"\n"
                + "description url = "+itemUrl+"\n"
                + "seller = "+sellerName+"\n"
                + "sellerUrl = "+sellerUrl+"\n"
                + "Current highest bid = "+price+"\n";
        if (bidHistory.size() >0)
        {
            str += "Bid history\n";
            for (Bid bid:bidHistory)
            {
                str += bid.toString();
            }
        }
        return str;
    }

    @Override
    public String[] getTableNames()
    {
        return new String[]{"BID_HISTORY"};
    }

    @Override
    public String[] getPrimaryKeyNames()
    {
        return new String[]{"itemId"};

    }

    @Override
    public Object[] getPrimaryKeyValues()
    {
        return new Object[]
            {itemId};
    }

    @Override
    public Class[] getPrimaryKeyTypes()
    {
        return new Class[]
                {Integer.class};
    }

    @Override
    public String[] getColumnNames() 
    {
        return new String[]

            {"itemId", "imgUrl", "title", "itemUrl", "seller", "sellerUrl" , "price"};
    }

    @Override
    public Object[] getColumnValues()
    {
        return new Object[]
            {itemId, imgUrl, title, itemUrl, sellerName, sellerUrl, price};
    }

    @Override
    public Class[] getColumnTypes()
    {
        return new Class[]
                {Integer.class, String.class, String.class, String.class,
                String.class, String.class, Double.class};
    }

    @Override
    public void initValues(Object[] values)
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public static void createTable()
    {
        System.out.println("creating BidHistory table");
        String stringToExecute = "CREATE TABLE BID_HISTORY ("
                + "itemId INTEGER NOT NULL PRIMARY KEY, imgUrl STRING, title STRING"
                + ", itemUrl STRING"
                + ", seller STRING"
                + ", sellerUrl STRING"
                + ", price DOUBLE)";
        Execute.execute(stringToExecute);
    }

    public Integer getItemId()
    {
        return itemId;
    }

    public void add(Bid bid)
    {
        bidHistory.add(bid);
    }



}
